package com.yyk.yykywd.ui;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.bean.IndexReportInfo;
import com.yyk.yykywd.config.HttpUrl;
import com.yyk.yykywd.http.Api;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.TimeUtil;
import com.yyk.yykywd.widght.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class YewutongzhiActivity extends BaseActivity {


    @BindView(R.id.rv_ywtz)
    RecyclerView rvYwtz;
    private BaseRecyclerAdapter<IndexReportInfo.DataBean> adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_yewutongzhi;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("业务通知");
    }

    @Override
    public void initData() {
        initAdapter();
        postIndexReport(true);
    }

    private void initAdapter() {
        adapter = new BaseRecyclerAdapter<IndexReportInfo.DataBean>(mActivity, list, R.layout.adapter_item_yewutongzhi) {
            @Override
            public void convert(BaseRecyclerHolder holder, IndexReportInfo.DataBean item, int position, boolean isScrolling) {
                holder.setText(R.id.tv_title, item.getTitle());
                holder.setText(R.id.tv_adminname, item.getAdmin_name());
                String time = TimeUtil.getInstance().getTime(item.getCreated_at(), TimeUtil.YYYY_MM_DD_HH_MM);
                holder.setText(R.id.tv_createtime, time + "");

            }
        };
        rvYwtz.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_f8f_10, 0));
        rvYwtz.setLayoutManager(new LinearLayoutManager(mActivity));
        rvYwtz.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                //TODO 业务通知详情 没有地址
                Intent intent = new Intent();
                intent.putExtra("url", HttpUrl.BASE_H5_URL + Api.CONTENT+ list.get(position).getId());
//                intent.putExtra("title", "业务通知");
                startAct(intent, WebActivity.class, null);
            }
        });

    }

    /**
     * 接口10.业务播报列表（ggk）
     */
    private List<IndexReportInfo.DataBean> list = new ArrayList<>();

    private void postIndexReport(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid") + "");
        map.put("page", 1 + "");
        map.put("num", 10000 + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postIndexReport(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<IndexReportInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(IndexReportInfo info) {
                        Logger.e("业务播报列表 --标题" + info.toString());
                        if (info.getCode() == 1) {
                            if (flag) {
                                list.clear();
                            }
                            if (info.getData() != null & info.getData().size() > 0) {
                                list.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
